package com.elinkway.infinitemovies.push;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.TaskStackBuilder;

import com.elinkway.infinitemovies.bean.JumpData;
import com.elinkway.infinitemovies.bean.Lottery;
import com.elinkway.infinitemovies.bean.PlayData;
import com.elinkway.infinitemovies.bean.PushData;
import com.elinkway.infinitemovies.ui.activity.NewMainActivity;
import com.elinkway.infinitemovies.ui.activity.SpreadActivity;
import com.elinkway.infinitemovies.ui.activity.TopicDetailActivity;
import com.elinkway.infinitemovies.ui.activity.VideoDetailActivity;
import com.elinkway.infinitemovies.ui.activity.play.PlayActivityFroWebView;
import com.elinkway.infinitemovies.ui.activity.play.PlayActivityLive;
import com.elinkway.infinitemovies.utils.Utils;

/**
 * 获取不同推送type的跳转Intent
 * @author liuliwei
 *
 */
public class PushIntentFactory {
	
	/**
	 * 由PushData 获取跳转Intent
	 * @param pushData
	 * @return
	 */
    public static PendingIntent getPendingIntentByType(Context context, PushData pushData) {
        String type = pushData.getType();
        if (type.equalsIgnoreCase(PushData.TYPE.ALBUM)) {
            return getAlbumPendingIntent(context, pushData);
        } else if (type.equalsIgnoreCase(PushData.TYPE.TOPIC)) {
            return getTopicPendingIntent(context, pushData);
        } else if (type.equalsIgnoreCase(PushData.TYPE.HTML)) {
            return getHtmlPendingIntent(context, pushData);
        } else if (type.equalsIgnoreCase(PushData.TYPE.ACTIVITY)) {
            return getActivityPendingIntent(context, pushData);
        } else if (type.equalsIgnoreCase(PushData.TYPE.LIVE)) {
            return getLiveActivityPendingInteng(context, pushData);
        } else {
            return getDefaultPendingIntent(context, pushData);
        }
    }

	/**
	 * 默认跳转首页
	 * @param pushData
	 * @return
	 */
	private static PendingIntent getDefaultPendingIntent(Context context,PushData pushData) {
		Intent i = new Intent(context, NewMainActivity.class);
		i.putExtras(getDefaultBundle());
				PendingIntent contentIntent = PendingIntent.getActivity(  
						context,  
			            0,  
			            i,   
			            PendingIntent.FLAG_CANCEL_CURRENT);  
		return contentIntent;
	}

	/**
	 * 跳转HTML页面
	 * @param pushData
	 * @return
	 */
	private static PendingIntent getHtmlPendingIntent(Context context,PushData pushData) {

		Bundle data = getDefaultBundle();
		data.putString("url", pushData.getResid());
		data.putString("title", pushData.getTitle());

		return getIntentStack(context,PlayActivityFroWebView.class,data);
	}

	/**
	 * 跳转专题页
	 * @param pushData
	 * @return
	 */
	private static PendingIntent getTopicPendingIntent(Context context,PushData pushData) {
		Bundle data = getDefaultBundle();
		JumpData jd =new JumpData();
		jd.setThemeid(pushData.getResid());
		jd.setName(pushData.getTitle());
		data.putSerializable(Utils.THEME_DATA, jd);

		return getIntentStack(context,TopicDetailActivity.class,data);
	}

	/**
	 * 跳转专辑页
	 * @param pushData
	 * @return
	 */
	private static PendingIntent getAlbumPendingIntent(Context context,PushData pushData) {
		Bundle data = getDefaultBundle();
		JumpData jumpData = new JumpData();
		jumpData.setAid(pushData.getResid());
		jumpData.setName(pushData.getTitle());
		data.putSerializable(Utils.ALBUM_DATA, jumpData);
		return getIntentStack(context,VideoDetailActivity.class,data);
	}
	
	private static PendingIntent getActivityPendingIntent(Context context, PushData pushData) {
		Bundle data = getDefaultBundle();
		Lottery lottery = new Lottery();
		lottery.setUrl(pushData.getResid());
		lottery.setId(98);
		data.putSerializable("lottery", lottery);
		return getIntentStack(context,SpreadActivity.class,data);
	}
	
    private static PendingIntent getLiveActivityPendingInteng(Context context, PushData pushData) {
        Bundle data = new Bundle();
        PlayData playdata = new PlayData();
        playdata.setmViewName(pushData.getTitle());
        playdata.setLiveStreams(pushData.getLiveStreams());
        data.putSerializable(Utils.PLAY_DATA, playdata);
        return getIntentStack(context, PlayActivityLive.class, data);
    }
	
	/**
	 * 创建跳转栈，存放两个Intent 栈顶为当前显示Activity，栈底为MainActivity
	 * @param context 
	 * @param cls 需要创建父Activity的Activity，一般为需要跳转的Activity
	 * @param b 跳转当前Activity需要的数据
	 * @return
	 */
	private static PendingIntent getIntentStack(Context context,Class<?> cls,Bundle b){
		TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
		Intent i = new Intent(context, cls);
		i.putExtras(b);
		// 压入父Activity
		stackBuilder.addParentStack(cls);
		// 压入当前Intent
		stackBuilder.addNextIntent(i);
		PendingIntent contentIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT);
		return contentIntent;
	}
	/**
	 * 跳转公共参数获取
	 * @return
	 */
	private static Bundle getDefaultBundle(){
		Bundle data = new Bundle();
		data.putBoolean("from_push_notification", true);
		return data;
	}
}
